import router from "@/router"
import { watch, ref } from "vue"
import { defineStore } from 'pinia'
import { ElMessage } from "element-plus"

export const useTabStore = defineStore('tab', () => {
    const arr = ref([
        {
            label: '电影管理',
            path: '/fsdy/film',
        }
    ])
    const curPath = ref(arr.value[0].path)


    watch(curPath, v => {
        console.log(v)
        router.push(v)
    })
    const add = (row) => {
        let index = arr.value.findIndex(item => item.path === row.path)
        if (index === -1) {
            arr.value.push(row)
        }
        curPath.value = row.path
    }

    const removeTab = (path) => {
        if (arr.length == 1) {
            ElMessage.info('至少保留一个选项卡')
            return
        }
        let index = arr.value.findIndex(item => item.path === path)
        if (index > -1) {
            arr.value.splice(index, 1)
            let len = arr.value.length
            if (len == 1) {
                curPath.value = arr.value[len - 1].path
            }
        }
    }

    return {
        arr,
        add,
        curPath,
        removeTab
    }
}, {
    persist: true
})